package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 华夏时报
 * @author Persh
 *
 */
public class ChinatimesNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.chinatimes.cc/article/[0-9]*.html",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}


	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		Document doc=Jsoup.parse(htmltxt);
		NewsMeta news=new NewsMeta();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		title =doc.select("div.news_title h3").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("h1").text();
		}
		content=HtmlCleaner.getContentHtml(url ,doc.select("div.infoMain"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url ,doc.select("div.c_content"));
		}
		date=DateUtils.matchDate(doc.select("div.news_laiyuan").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.info").text());
		}
		String author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setAuthor(author);
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		ChinatimesNewsAnalyse analyse=new ChinatimesNewsAnalyse();
		String url="http://www.chinatimes.cc/article/62108.html";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.err.println(analyse.isDetailPage(url));
		//System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
